package com.tensquare.articlecrawler.pipeline;

import com.tensquare.articlecrawler.dao.ArticleDao;
import com.tensquare.articlecrawler.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import util.IdWorker;
import java.util.Date;
/**
 * 文章入库类
 */
@Component
public class ArticleDbPipeline implements Pipeline{

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private IdWorker idWorker;

    private String channelId;//频道ID

    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
       //1.从PageProcessor获取内容
        String title = (String)resultItems.get("title");
        String content = (String)resultItems.get("content");

        //2.把内容存入mysql
        Article article = new Article();
        article.setId(idWorker.nextId()+"");
        article.setTitle(title);
        article.setContent(content);
        article.setCreatetime(new Date());
        article.setChannelid(channelId);

        articleDao.save(article);
    }
}
